package ixj_xmlprocess;

import ixj_entity.Customer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author LamHuynh
 */
public class CustomerXMLProcess {

    private String fileName;

    public CustomerXMLProcess(String fileName) {
        this.fileName = fileName;
    }

    private void processAllNode(Node node, List<Customer> result) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = node.getNodeName();

            if ("customer".equalsIgnoreCase(nodeName)) {
                NamedNodeMap attrs = node.getAttributes();
                String id = attrs.getNamedItem("id").getNodeValue();
                String fname = attrs.getNamedItem("fname").getNodeValue();

                Customer cus = new Customer();
                cus.setId(id);
                cus.setFname(fname);

                result.add(cus);
            }
        }

        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            processAllNode(childNodes.item(i), result);
        }
    }
    /*
    ////////
    private void processNode(Node node, Customer customer) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
    String nodeName = node.getNodeName();
    if ("id".equalsIgnoreCase(nodeName)) {
    String id = node.getChildNodes().item(0).getNodeValue();
    customer.setId(id);
    } else if ("fname".equalsIgnoreCase(nodeName)) {
    String fname = node.getChildNodes().item(0).getNodeValue();
    customer.setFname(fname);
    }
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
    processNode(childNodes.item(i), customer);
    }

    }
    //////
    private void processAllNode(Node node, List<Customer> result) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
    String nodeName = node.getNodeName();
    if ("customer".equalsIgnoreCase(nodeName)) {
    Customer customer = new Customer();
    processNode(node, customer);
    result.add(customer);
    }
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
    processAllNode(childNodes.item(i), result);
    }
    }
     */
    /////

    public List<Customer> getAllCustomer() {
        try {
            List<Customer> result = new ArrayList<Customer>();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = factory.newDocumentBuilder();
            Document doc = parser.parse(new File(this.fileName));


            processAllNode(doc, result);
            //

            return result;
        } catch (IOException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            ex.printStackTrace();
        }

        return null;
    }
}
